package l2hc.gameserver.database;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

import javolution.util.FastMap;
import l2hc.gameserver.model.actor.instances.player.FakePc;

public class FakePcsTable
{
	private static Logger _log = Logger.getLogger(FakePcsTable.class.getName());
	
	private FastMap<Integer, FakePc> _fakePcs = new FastMap<Integer, FakePc>();
	private static FakePcsTable _instance;
	private FakePcsTable()
	{
               loadData();
	}
	
	private void loadData()
	{
		_fakePcs.clear();
		
		Connection con = null;
		
		try
		{
			con = DatabaseFactory.getInstance().getConnection();
			
			PreparedStatement stmt = con.prepareStatement("SELECT * FROM `fake_pcs`");
			ResultSet rset = stmt.executeQuery();
			
			FakePc fpc = null;
			
			while (rset.next())
			{
				fpc = new FakePc();
				
				int npcId = rset.getInt("npc_id");
				fpc._race = rset.getInt("race");
				fpc._sex = rset.getInt("sex");
				fpc.base_class = rset.getInt("class");
				fpc._title = rset.getString("title");
				fpc._title_color = Integer.decode("0x" + rset.getString("title_color"));
				fpc._name = rset.getString("name");
				fpc._nameColor = Integer.decode("0x" + rset.getString("name_color"));
				fpc.hair_style = rset.getInt("hair_style");
				fpc.hair_color = rset.getInt("hair_color");
				fpc.face = rset.getInt("face");
				fpc.mount_type = rset.getByte("mount");
				//fpc._team = rset.getByte("team");
				fpc._hero = rset.getByte("hero");
				fpc.pdUnder = rset.getInt("pd_under");
				fpc.pdUnderAug = rset.getInt("pd_under_aug");
				fpc.pdHead = rset.getInt("pd_head");
				fpc.pdHeadAug = rset.getInt("pd_head_aug");
				fpc.pdRHand = rset.getInt("pd_rhand");
				fpc.pdRHandAug = rset.getInt("pd_rhand_aug");
				fpc.pdLHand = rset.getInt("pd_lhand");
				fpc.pdLHandAug = rset.getInt("pd_lhand_aug");
				fpc.pdGloves = rset.getInt("pd_gloves");
				fpc.pdGlovesAug = rset.getInt("pd_gloves_aug");
				fpc.pdChest = rset.getInt("pd_chest");
				fpc.pdChestAug = rset.getInt("pd_chest_aug");
				fpc.pdLegs = rset.getInt("pd_legs");
				fpc.pdLegsAug = rset.getInt("pd_legs_aug");
				fpc.pdFeet = rset.getInt("pd_feet");
				fpc.pdFeetAug = rset.getInt("pd_feet_aug");
				fpc.pdBack = rset.getInt("pd_back");
				fpc.pdBackAug = rset.getInt("pd_back_aug");
				fpc.pdLRHand = rset.getInt("pd_lrhand");
				fpc.pdLRHandAug = rset.getInt("pd_lrhand_aug");
				fpc.pdHair = rset.getInt("pd_hair");
				fpc.pdHairAug = rset.getInt("pd_hair_aug");
				fpc.pdHair2 = rset.getInt("pd_hair2");
				fpc.pdHair2Aug = rset.getInt("pd_hair2_aug");
				fpc.pdRBracelet = rset.getInt("pd_rbracelet");
				fpc.pdRBraceletAug = rset.getInt("pd_rbracelet_aug");
				fpc.pdLBracelet = rset.getInt("pd_lbracelet");
				fpc.pdLBraceletAug = rset.getInt("pd_lbracelet_aug");
				fpc.pdDeco1 = rset.getInt("pd_deco1");
				fpc.pdDeco1Aug = rset.getInt("pd_deco1_aug");
				fpc.pdDeco2 = rset.getInt("pd_deco2");
				fpc.pdDeco2Aug = rset.getInt("pd_deco2_aug");
				fpc.pdDeco3 = rset.getInt("pd_deco3");
				fpc.pdDeco3Aug = rset.getInt("pd_deco3_aug");
				fpc.pdDeco4 = rset.getInt("pd_deco4");
				fpc.pdDeco4Aug = rset.getInt("pd_deco4_aug");
				fpc.pdDeco5 = rset.getInt("pd_deco5");
				fpc.pdDeco5Aug = rset.getInt("pd_deco5_aug");
				fpc.pdDeco6 = rset.getInt("pd_deco6");
				fpc.pdDeco6Aug = rset.getInt("pd_deco6_aug");
				fpc._enchant = rset.getInt("enchant_effect");
				//fpc.pvpFlag = rset.getInt("pvp_flag");
				fpc.karma = rset.getInt("karma");
				fpc._fishing = rset.getByte("fishing");
				//fpc._fishLoc.x = rset.getInt("fishing_x");
				//fpc._fishLoc.y = rset.getInt("fishing_y");
				//fpc._fishLoc.z = rset.getInt("fishing_z");
				_fakePcs.put(npcId, fpc);
			}
			
			rset.close();
			stmt.close();
		}
		catch (SQLException e)
		{
			_log.log(Level.SEVERE, "Error while creating fake pc table: " + e.getMessage(), e);
		}
		finally
		{
			DatabaseFactory.close(con);
		}
	}
	
	public void reloadData()
	{
		loadData();
	}	
      	
	public FakePc getFakePc(int npcId)
	{
		return _fakePcs.get(npcId);
	}	

	public static FakePcsTable getInstance()
	{
		if(_instance == null)
			_instance = new FakePcsTable();
		return _instance;
	}

}